package com.viphuli.activity;

import java.lang.ref.WeakReference;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.offroader.core.OffRoaderFragmentActivity;
import com.viphuli.app.R;
import com.viphuli.common.MyPageHelper;
import com.viphuli.fragment.BaseProgressFragment;

public class MyActionBarActivity extends OffRoaderFragmentActivity implements OnClickListener {

	public final static String BUNDLE_KEY_PAGE = "BUNDLE_KEY_PAGE";
	public final static String BUNDLE_KEY_ARGS = "BUNDLE_KEY_ARGS";

	public static final String BUNDLE_KEY_TITLE = "display_title";

	protected WeakReference<Fragment> mFragment;
	protected int mPageValue = -1;
	protected ActionBar actionBar;
	private ViewGroup backBtn;
	private TextView titlTv;
	private ViewGroup rightBtn;
	private TextView rightText;
	private BaseProgressFragment frag;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	public int getContentView() {
		return R.layout.act_my_title;
	}

	@Override
	public void initView() {

		actionBar = getSupportActionBar();
		if (actionBar != null) {
			actionBar.setTitle("");
			//去阴影
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
				actionBar.setElevation(25);
			}
		}

		//自定义ActionBar
		actionBar.setCustomView(R.layout.common_title_bar);
		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		actionBar.setDisplayShowCustomEnabled(true);
		
		backBtn = (ViewGroup) actionBar.getCustomView().findViewById(R.id.id_common_bar_back_btn);
		rightBtn = (ViewGroup) actionBar.getCustomView().findViewById(R.id.id_common_bar_right_btn);
		rightText = (TextView) actionBar.getCustomView().findViewById(R.id.id_common_bar_right_text);
		titlTv = (TextView) actionBar.getCustomView().findViewById(R.id.id_common_bar_title);

		rightBtn.setVisibility(View.GONE);

		backBtn.setOnClickListener(this);

		//初始化参数
		if (mPageValue == -1) {
			mPageValue = getIntent().getIntExtra(BUNDLE_KEY_PAGE, 0);
		}

		initFromIntent(mPageValue, getIntent());

	}
	
	@Override
	public void initData() {
	}

	protected void initFromIntent(int pageValue, Intent data) {
		if (data == null)
			throw new RuntimeException("you must provide a page info to display");

		MyPageHelper page = MyPageHelper.getPageByValue(pageValue);
		if (page == null)
			throw new IllegalArgumentException("can not find page by value:" + pageValue);

		Bundle args = data.getBundleExtra(BUNDLE_KEY_ARGS);
		if (args != null && args.containsKey(BUNDLE_KEY_TITLE)) {
			setActionBarTitle(args.getString(BUNDLE_KEY_TITLE));
		} else
			setActionBarTitle(page.getTitle());

		try {
			Fragment fragment = (Fragment) page.getClz().newInstance();

			if (args != null) {
				fragment.setArguments(args);
			}

			FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
			trans.replace(R.id.container, fragment);
			trans.commitAllowingStateLoss();

			mFragment = new WeakReference<Fragment>(fragment);
		} catch (Exception e) {
			e.printStackTrace();
			throw new IllegalArgumentException("generate fragment error. by value:" + pageValue);
		}
	}

	/**
	 * 设置标题
	 * @param title
	 */
	public void setActionBarTitle(String title) {
		if (actionBar != null) {
			actionBar.setTitle(title);

			titlTv.setText(title);
		}
	}

	/**
	 * 设置标题
	 * @param resId
	 */
	public void setActionBarTitle(int resId) {
		if (resId != 0) {
			setActionBarTitle(getString(resId));
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.id_common_bar_back_btn:
			finish();
			break;

		default:
			break;
		}

	}

	@Override
	public void onAttachFragment(Fragment fragment) {
		super.onAttachFragment(fragment);

		//同Fragment通信，接口穿透
		if (fragment instanceof BaseProgressFragment)
			frag = (BaseProgressFragment) fragment;
		else
			throw new RuntimeException("必需使用BaseProgressFragment做为父类");
	}

	/**
	 * 监听返回--是否退出程序
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			frag.onBackPressed();
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

	public ViewGroup getRightBtn() {
		return rightBtn;
	}

	public TextView getRightText() {
		return rightText;
	}

	public ViewGroup getBackBtn() {
		return backBtn;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		BaseProgressFragment frag = (BaseProgressFragment) getSupportFragmentManager().findFragmentById(R.id.container);
		frag.onActivityResult(requestCode, resultCode, data);
	}

}
